﻿Imports Exp.DATA_Service
Imports Exp.Model
Imports Exp.DATA_Service.Exp.DATA_Service

Public Class Owner
    Inherits BasePage
    Const MENUNAME As String = "OWNER"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                Dim objUser As REM_User = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(objUser.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

            End If
            BindData()
            BindConTrolSearch()
            txtMobileNumberSearch.Text = ""
            txtLocationSearch.Text = ""
            txtFirstNameSearch.Text = ""
            txtLastNameSearch.Text = ""
        End If
    End Sub

#Region "Function"
    Sub Save()
        If Me.ddl_User.SelectedValue.ToString.Trim = "" Then
            JavaScript.AlertMessage(Me.Page, "Please select owner.")
            Return
        End If
        If Me.ddl_Unit.SelectedValue.ToString.Trim = "" Then
            JavaScript.AlertMessage(Me.Page, "Please select Unit number.")
            Return
        End If

        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        Dim Owner_Service As New Owner_Service
        Dim objREM_Owner As New REM_Owner
        'If txtQuota.Text <> "" Then
        If CheckSaveExisting(ddl_User.SelectedValue, ddl_Unit.SelectedValue) Then
            JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
            Return
        End If
        objREM_Owner.User_ID = ddl_User.SelectedValue
        objREM_Owner.Unit_ID = ddl_Unit.SelectedValue
        If txtQuota.Text.Trim <> "" Then objREM_Owner.Quota = txtQuota.Text
        objREM_Owner.CreateDate = CType(Now, Date)
        'objREM_Owner.UpdateDate = CType(Now, Date)
        objREM_Owner.CreateUser = objUser.User_ID
        'objREM_Owner.UpdateUser = objUser.User_ID
        Owner_Service.Add(objREM_Owner)
        JavaScript.AlertMessage(Me.Page, "Save data complete")
        'Else
        '    JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        'End If
    End Sub
    Sub Edit()
        Dim objUser As REM_User = CType(Session("USER"), REM_User)
        Dim Owner_Service As New Owner_Service
        Dim objREM_Owner As New REM_Owner
        If txtQuota.Text <> "" Then
            'If CheckSaveExisting(ddl_User.SelectedValue, ddl_Unit.SelectedValue) Then
            '    JavaScript.AlertMessage(Me.Page, "Data existing, please try again")
            '    Return
            'End If
            objREM_Owner.User_ID = CType(HDFieldUserID.Value, Integer)
            objREM_Owner.Unit_ID = CType(HDFieldUnitID.Value, Integer)
            objREM_Owner.Quota = txtQuota.Text
            objREM_Owner.UpdateDate = CType(Now, Date)
            objREM_Owner.UpdateUser = objUser.User_ID
            Owner_Service.Update(objREM_Owner)
            JavaScript.AlertMessage(Me.Page, "Save data complete")
        Else
            JavaScript.AlertMessage(Me.Page, "Please insert data, try again")
        End If
    End Sub
    Sub BindData()
        Dim Owner_Service As New Owner_Service
        Dim lEXP_Select_REM_Owner_ByUserID_Result As New List(Of EXP_Select_REM_Owner_ByUserID_Result)
        lEXP_Select_REM_Owner_ByUserID_Result = Owner_Service.GetAll()
        gv_data.DataSource = lEXP_Select_REM_Owner_ByUserID_Result
        gv_data.DataBind()
    End Sub
    Sub BindDataSearch()
        Dim Unit_Service As New Unit_Service
        'Dim lEXP_Select_REM_Unit_And_REM_Resort_Result As New List(Of EXP_Select_REM_Unit_And_REM_Resort_Result)
        'lEXP_Select_REM_Unit_And_REM_Resort_Result = Unit_Service.GetAllData()
        Dim Owner_Service As New Owner_Service
        Dim lstResult As New List(Of EXP_Select_REM_Owner_ByUserID_Result)
        lstResult = Owner_Service.GetAll()
        'Dim lstResult = Unit_Service.GetAllData().ToList()
        If ddlBrandSearch.SelectedValue <> "" Then
            lstResult = lstResult.Where(Function(p) p.Brand_ID = ddlBrandSearch.SelectedValue).ToList()
        End If
        If ddlResortSearch.SelectedValue <> "" Then
            lstResult = lstResult.Where(Function(p) p.Resort_ID = ddlResortSearch.SelectedValue).ToList()
        End If
        If ddlCountrySearch.SelectedValue <> "" Then
            lstResult = lstResult.Where(Function(p) p.CountryName = ddlCountrySearch.Text).ToList()
        End If
        If ddlUserSearch.SelectedValue <> "" Then
            lstResult = lstResult.Where(Function(p) p.User_ID = ddlUserSearch.SelectedValue).ToList()
        End If
        If (txtMobileNumberSearch.Text.Trim <> "") Then
            lstResult = lstResult.Where(Function(p) Not p.MobilePhone Is Nothing).ToList()
            lstResult = lstResult.Where(Function(p) p.MobilePhone.ToUpper.Contains(txtMobileNumberSearch.Text.Trim.ToUpper)).ToList()
        End If
        If txtFirstNameSearch.Text.Trim <> "" Then
            lstResult = lstResult.Where(Function(p) Not p.FirstName Is Nothing).ToList()
            lstResult = lstResult.Where(Function(p) p.FirstName.ToUpper.Contains(txtFirstNameSearch.Text.Trim.ToUpper)).ToList()
        End If
        If txtLastNameSearch.Text.Trim <> "" Then
            lstResult = lstResult.Where(Function(p) Not p.LastName Is Nothing).ToList()
            lstResult = lstResult.Where(Function(p) p.LastName.ToUpper.Contains(txtLastNameSearch.Text.Trim.ToUpper)).ToList()
        End If
        If txtLocationSearch.Text.Trim <> "" Then
            lstResult = lstResult.Where(Function(p) Not p.LastName Is Nothing).ToList()
            lstResult = lstResult.Where(Function(p) p.Address.ToUpper.Contains(txtLocationSearch.Text.Trim.ToUpper)).ToList()
        End If
        gv_data.DataSource = lstResult
        gv_data.DataBind()
    End Sub
    Sub BindConTrol()
        BindUser()
        Dim iBrandID As Integer
        Dim iResortID As Integer
        BindBrand()
        'iBrandID = ddl_Brand.SelectedValue
        BindResort()
        ' iResortID = ddl_Resort.SelectedValue
        BindUnit()
    End Sub
    Sub BindConTrolSearch()
        BindBrandSearch()
        BindCountrySearch()
        BindUserSearch()
        BindResortSearch()
    End Sub
    Sub BindBrand()
        ddl_Brand.Items.Clear()

        Dim REM_Brand As New List(Of REM_Brand)
        Dim Brand_Service As New Brand_Service
        REM_Brand = Brand_Service.GetAll()
        REM_Brand.Distinct()
        ddl_Brand.DataSource = REM_Brand
        ddl_Brand.DataTextField = "BrandName"
        ddl_Brand.DataValueField = "Brand_ID"
        ddl_Brand.DataBind()
        ddl_Brand.Items.Insert(0, New ListItem("", ""))

    End Sub
    Sub BindResort(Optional ByVal ibrandID As Integer = 0)
        ddl_Resort.Items.Clear()

        Dim REM_Resort As New List(Of REM_Resort)
        Dim Resort_Service As New Resort_service
        REM_Resort = Resort_Service.GetAll()
        If ibrandID <> 0 Then REM_Resort = REM_Resort.Where(Function(p) p.Brand_ID = ibrandID).ToList()
        REM_Resort.Distinct()
        ddl_Resort.DataSource = REM_Resort
        ddl_Resort.DataTextField = "ResortName"
        ddl_Resort.DataValueField = "Resort_ID"
        ddl_Resort.DataBind()
        ddl_Resort.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindUnit(Optional ByVal iResortID As Integer = 0)

        ddl_Unit.Items.Clear()

        'Dim REM_Unit As New List(Of REM_Unit)
        Dim Unit_Service As New Unit_Service
        Dim ownSv As New Owner_Service
        Dim listOwn = ownSv.GetREM_Owner

        Dim REM_Unit = Unit_Service.GetAllUnit_NotInOwner



        'If Me.ddl_User.SelectedIndex >= 0 Then
        '    Dim user_id As Integer = CInt(ddl_User.SelectedValue.ToString)
        '    Dim liunit = From p In REM_Unit Where listOwn.Contains(p.REM_Owner) Select p
        '    REM_Unit = liunit.ToList
        '    REM_Unit = REM_Unit.Where(Function(p) p.Resort_ID = iResortID).ToList
        '    'REM_Unit = REM_Unit.Where(Function(r) r.Unit_ID).Contains(listOwn.Where(Function(o) o.Unit_ID))
        'Else

        '    REM_Unit = REM_Unit.Where(Function(p) p.Resort_ID = iResortID).ToList
        'End If

        If iResortID <> 0 Then REM_Unit = REM_Unit.Where(Function(p) p.Resort_ID = iResortID).ToList


        REM_Unit.Distinct()
        ddl_Unit.DataSource = REM_Unit
        ddl_Unit.DataTextField = "UnitName"
        ddl_Unit.DataValueField = "Unit_ID"
        ddl_Unit.DataBind()
        ddl_Unit.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindUser()
        ddl_User.Items.Clear()
        'ddl_User.Items.Add(New ListItem("", ""))

        Dim EXP_Select_REM_User As New List(Of EXP_Select_REM_User_Result)
        Dim User_Service As New User_Service
        EXP_Select_REM_User = User_Service.GetAll()

        EXP_Select_REM_User = EXP_Select_REM_User.Where(Function(p) p.Role_ID = 3).ToList()
        EXP_Select_REM_User.Distinct()


        ddl_User.DataSource = EXP_Select_REM_User
        ddl_User.DataTextField = "FullName"
        ddl_User.DataValueField = "User_ID"
        ddl_User.DataBind()
        ddl_User.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindBrandSearch()
        Dim REM_Brand As New List(Of REM_Brand)
        Dim Brand_Service As New Brand_Service
        REM_Brand = Brand_Service.GetAll()
        REM_Brand.Distinct()
        ddlBrandSearch.DataSource = REM_Brand
        ddlBrandSearch.DataTextField = "BrandName"
        ddlBrandSearch.DataValueField = "Brand_ID"
        ddlBrandSearch.DataBind()
        ddlBrandSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindResortSearch()
        Dim REM_Resort As New List(Of REM_Resort)
        Dim Resort_Service As New Resort_service
        REM_Resort = Resort_Service.GetAll()
        REM_Resort.Distinct()
        ddlResortSearch.DataSource = REM_Resort
        ddlResortSearch.DataTextField = "ResortName"
        ddlResortSearch.DataValueField = "Resort_ID"
        ddlResortSearch.DataBind()
        ddlResortSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindCountrySearch()
        Dim REM_Country As New List(Of REM_Country)
        Dim Country_Service As New Country_Service
        REM_Country = Country_Service.GetAll()
        REM_Country.Distinct()
        ddlCountrySearch.DataSource = REM_Country
        ddlCountrySearch.DataTextField = "CountryName"
        ddlCountrySearch.DataValueField = "Country_ID"
        ddlCountrySearch.DataBind()
        ddlCountrySearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Sub BindUserSearch()
        Dim EXP_Select_REM_User_Result As New List(Of EXP_Select_REM_User_Result)
        Dim User_Service As New User_Service
        Dim datenow As Date = Now.Date
        EXP_Select_REM_User_Result = User_Service.GetAll()
        EXP_Select_REM_User_Result = EXP_Select_REM_User_Result.Where(Function(p) p.Role_ID = 3).ToList()
        EXP_Select_REM_User_Result = EXP_Select_REM_User_Result.Where(Function(p) p.ExpireDate Is Nothing OrElse p.ExpireDate < datenow).ToList
        EXP_Select_REM_User_Result.Distinct()
        ddlUserSearch.DataSource = EXP_Select_REM_User_Result
        ddlUserSearch.DataTextField = "UserName"
        ddlUserSearch.DataValueField = "User_ID"
        ddlUserSearch.DataBind()
        ddlUserSearch.Items.Insert(0, New ListItem("", ""))
    End Sub
    Function CheckSaveExisting(ByVal iUserID As Integer, ByVal iUnitID As Integer) As Boolean
        Dim Owner_Service As New Owner_Service
        Dim LobjREM_Owner As New List(Of REM_Owner)

        LobjREM_Owner = Owner_Service.GetByUserIDAndUnitID(iUserID, iUnitID)
        If LobjREM_Owner Is Nothing Then
            Return False
        Else
            If LobjREM_Owner.Count > 0 Then
                Return True
            End If
        End If
        Return False
    End Function
    Function CheckInsert(ByRef sError As String) As Boolean
        CheckInsert = True
        If ddl_User.SelectedValue = "" Then
            sError = "Please insert owner name,"
        ElseIf ddl_Unit.SelectedValue = "" Then
            sError &= "Please insert owner name Unit ,"
        ElseIf (txtQuota.Text.Trim <> "" AndAlso CInt(txtQuota.Text) > 365) Then
            sError &= "Quota over 365 day,"
        End If
        If sError.Length > 1 Then
            CheckInsert = False
            sError = sError.Substring(1, sError.Length - 1)
        End If
    End Function

#End Region
#Region "Event"

    Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        ddl_Brand.Enabled = True
        ddl_Resort.Enabled = True
        ddl_Unit.Enabled = True
        ddl_User.Enabled = True
        HDFieldUnitID.Value = ""
        HDFieldUserID.Value = ""
        BindConTrol()
    End Sub
    Private Sub ddl_Brand_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_Brand.SelectedIndexChanged
        BindResort(ddl_Brand.SelectedValue)

        If ddl_Resort.SelectedValue = "" Then
            BindUnit(0)
        Else
            BindUnit(ddl_Resort.SelectedValue)
        End If
    End Sub
    Private Sub ddl_Resort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddl_Resort.SelectedIndexChanged
        If ddl_Resort.SelectedValue = "" Then
            BindUnit(0)
        Else
            BindUnit(ddl_Resort.SelectedValue)
        End If
    End Sub
    Private Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        BindData()
        HDFieldUnitID.Value = ""
        HDFieldUserID.Value = ""
    End Sub
    Private Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        Dim sError As String = ""

        If HDFieldUnitID.Value = "" Then
            If CheckInsert(sError) Then
                Save()
                BindData()
                MultiView1.ActiveViewIndex = 0
            End If
        Else
            If CheckInsert(sError) Then
                Edit()
                BindData()
                MultiView1.ActiveViewIndex = 0
            End If
        End If
        HDFieldUnitID.Value = ""
        HDFieldUserID.Value = ""
    End Sub
    Protected Sub txtFirstNameSearch_TextChanged(sender As Object, e As EventArgs) Handles txtFirstNameSearch.TextChanged

    End Sub
    Protected Sub txtMobileNumberSearch_TextChanged(sender As Object, e As EventArgs) Handles txtMobileNumberSearch.TextChanged

    End Sub
    Protected Sub txtLastNameSearch_TextChanged(sender As Object, e As EventArgs) Handles txtLastNameSearch.TextChanged

    End Sub
    Protected Sub txtLocationSearch_TextChanged(sender As Object, e As EventArgs) Handles txtLocationSearch.TextChanged

    End Sub
    Protected Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
        BindDataSearch()
    End Sub

    Protected Sub btnBackView_Click(sender As Object, e As EventArgs) Handles btnBackView.Click
        MultiView1.ActiveViewIndex = 0
        BindData()
    End Sub
#End Region
#Region "gv_Data"
    Private Sub gv_data_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_data.RowCommand
        Dim commandArgs() As String = e.CommandArgument.ToString().Split(",")
        Dim iUnitID As Integer = commandArgs(0)
        Dim iUserID As Integer = commandArgs(1)
        HDFieldUnitID.Value = iUnitID
        HDFieldUserID.Value = iUserID
        If e.CommandName.ToLower = "edit" Then
            Dim Owner_Service As New Owner_Service
            Dim lEXP_Select_REM_Owner_ByUserID As New List(Of EXP_Select_REM_Owner_ByUserID_Result)
            lEXP_Select_REM_Owner_ByUserID = Owner_Service.GetOwner_ByUserID(iUserID)
            lEXP_Select_REM_Owner_ByUserID = lEXP_Select_REM_Owner_ByUserID.Where(Function(p) p.Unit_ID = iUnitID).ToList()
            If Not lEXP_Select_REM_Owner_ByUserID Is Nothing Then
                BindConTrol()
                If lEXP_Select_REM_Owner_ByUserID.Item(0).Unit_ID > 0 Then
                    txtQuota.Text = lEXP_Select_REM_Owner_ByUserID.Item(0).Quota
                    ddl_Brand.SelectedValue = lEXP_Select_REM_Owner_ByUserID.Item(0).Brand_ID
                    ddl_Resort.SelectedValue = lEXP_Select_REM_Owner_ByUserID.Item(0).Resort_ID
                    ddl_Unit.SelectedValue = lEXP_Select_REM_Owner_ByUserID.Item(0).Unit_ID
                    ddl_User.SelectedValue = lEXP_Select_REM_Owner_ByUserID.Item(0).User_ID

                    ddl_Brand.Enabled = False
                    ddl_Resort.Enabled = False
                    ddl_Unit.Enabled = False
                    ddl_User.Enabled = False

                    MultiView1.ActiveViewIndex = 1

                End If
            End If
        ElseIf e.CommandName.ToLower = "delete" Then
            Dim Owner_Service As New Owner_Service
            Owner_Service.Delete(iUserID, iUnitID)
            BindData()
        ElseIf e.CommandName.ToLower = "detail" Then
            ucUnitView.UnitID = iUnitID
            ucUnitView.BindData()
            MultiView1.ActiveViewIndex = 2
        End If
    End Sub
    Private Sub gv_Data_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_data.RowDataBound
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then

                Dim user_id As Integer = CType(CType(e.Row.Cells(8).FindControl("lblUser_ID"), Label).Text, Integer)

                If GetDataUtil.checkIsMemberActive(user_id) = False Then
                    CType(e.Row.FindControl("LinkButton2"), LinkButton).Visible = True
                End If


                Dim UnitType_Service As New UnitType_service
                Dim User_Service As New User_Service
                Dim Brand_Service As New Brand_Service
                Dim sFName, sLName As String

                e.Row.Cells(6).Text = UnitType_Service.GetByID(CType(CType(e.Row.Cells(6).FindControl("lblUnitType_ID"), Label).Text, Integer)).UnitTypeName
                sFName = User_Service.GetByID(user_id).FirstName
                sLName = User_Service.GetByID(user_id).LastName
                e.Row.Cells(8).Text = sFName & " " & sLName
                e.Row.Cells(9).Text = Brand_Service.GetByID(CType(CType(e.Row.Cells(9).FindControl("lblBrand_ID"), Label).Text, Integer)).BrandName
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99CCFF'")
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;")

            End If

        Catch ex As Exception

            ' Page.ClientScript.AlertMessage(Me, "gv_Data_RowDataBound", "gv_Data_RowDataBound >>" & ex.Message)

        End Try
    End Sub
    Private Sub gv_data_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles gv_data.RowDeleting
        BindData()
    End Sub
    Private Sub gv_data_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gv_data.RowEditing
        BindData()
    End Sub
#End Region


End Class